package com.cljz.makepassenger.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.model.net.bean.NoticeListBean;
import com.cljz.makepassenger.present.activity.SystemMsgP;
import com.cljz.makepassenger.ui.adapter.SystemMsgListAdapter;
import com.cljz.makepassenger.utils.T;
import com.jakewharton.rxbinding.view.RxView;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/5/14.
 */

public class SystemMsgActivity extends BaseActivity<SystemMsgP> implements XRecyclerView.LoadingListener, SystemMsgListAdapter.OnItemClickListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.fl_title_right)
    FrameLayout flTitleRight;
    @BindView(R.id.iv_title_right1)
    ImageView ivTitleRight1;
    @BindView(R.id.fl_title_right1)
    FrameLayout flTitleRight1;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.xrv_system_msg_list)
    XRecyclerView xrvSystemMsgList;
    @BindView(R.id.tv_check_all)
    TextView tvCheckAll;
    @BindView(R.id.btn_delete)
    Button btnDelete;
    @BindView(R.id.rl_delete)
    RelativeLayout rlDelete;
    private ArrayList<NoticeListBean.DataBeanX> noticeList;
    private boolean isDelete = false;
    private SystemMsgListAdapter systemMsgListAdapter;
    private int page = 1;
    private boolean isLoadMore;
    private boolean isRefresh;
    private boolean hasCheckAll = false;

    @Override
    public void initData(Bundle savedInstanceState) {
        noticeList = (ArrayList<NoticeListBean.DataBeanX>) getIntent().getExtras().getSerializable("noticeList");
        tvTitleName.setText("系统通知");
        tvTitleRight.setText("删除");
        tvTitleRight.setTextColor(getResources().getColor(R.color.text_color));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xrvSystemMsgList.setLayoutManager(linearLayoutManager);
        systemMsgListAdapter = new SystemMsgListAdapter(this, noticeList, isDelete);
        xrvSystemMsgList.setAdapter(systemMsgListAdapter);
        xrvSystemMsgList.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);
        xrvSystemMsgList.setPullRefreshEnabled(true);
        xrvSystemMsgList.setLoadingMoreEnabled(true);
    }


    @Override
    protected void initListener() {
        super.initListener();
        xrvSystemMsgList.setLoadingListener(this);
        xrvSystemMsgList.refresh();
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.pop(SystemMsgActivity.this);
                    }
                });
        RxView.clicks(flTitleRight)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (isDelete) {
                            rlDelete.setVisibility(View.GONE);
                            tvTitleRight.setText("删除");
                            isDelete = false;
                            xrvSystemMsgList.setPullRefreshEnabled(true);
                            xrvSystemMsgList.setLoadingMoreEnabled(true);
                        } else {
                            rlDelete.setVisibility(View.VISIBLE);
                            tvTitleRight.setText("取消");
                            isDelete = true;
                            xrvSystemMsgList.setPullRefreshEnabled(false);
                            xrvSystemMsgList.setLoadingMoreEnabled(false);
                        }
                        for (int i = 0; i < noticeList.size(); i++) {
                            NoticeListBean.DataBeanX dataBeanX = noticeList.get(i);
                            dataBeanX.setHasSelected(false);
                            noticeList.set(i, dataBeanX);
                        }
                        systemMsgListAdapter.setDeleteAndData(isDelete, noticeList);
                    }
                });
        RxView.clicks(tvCheckAll)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (hasCheckAll) {
                            tvCheckAll.setText("全选");
                            for (int i = 0; i < noticeList.size(); i++) {
                                NoticeListBean.DataBeanX dataBeanX = noticeList.get(i);
                                dataBeanX.setHasSelected(false);
                                noticeList.set(i, dataBeanX);
                            }
                            hasCheckAll = false;
                        } else {
                            tvCheckAll.setText("取消全选");
                            for (int i = 0; i < noticeList.size(); i++) {
                                NoticeListBean.DataBeanX dataBeanX = noticeList.get(i);
                                dataBeanX.setHasSelected(true);
                                noticeList.set(i, dataBeanX);
                            }
                            hasCheckAll = true;
                        }
                        systemMsgListAdapter.setData(noticeList);
                    }
                });
        RxView.clicks(btnDelete)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        StringBuilder deleteId = new StringBuilder();
                        if (noticeList.size() > 0) {
                            for (int index = noticeList.size() - 1; index >= 0; index--) {
                                NoticeListBean.DataBeanX dataBeanX = noticeList.get(index);
                                if (dataBeanX.isHasSelected()) {
                                    deleteId.append(dataBeanX.getId());
                                    deleteId.append(",");
                                }
                            }
                            if (deleteId.length() > 0) {
                                String delete = deleteId.substring(0, deleteId.length() - 1);
                                getP().noticeDelete(delete);
                            } else {
                                T.getInstance().showShort("未选中任何通知");
                            }
                        }
                    }
                });
        systemMsgListAdapter.setOnItemClickListener(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_system_msg_activity;
    }

    @Override
    public SystemMsgP newP() {
        return new SystemMsgP();
    }

    @Override
    public void onRefresh() {
        page = 1;
        isRefresh = true;
        getP().getNoticeList(page);
    }

    @Override
    public void onLoadMore() {
        page++;
        isLoadMore = true;
        getP().getNoticeList(page);
    }

    public void getNoticeListSucceed(ArrayList<NoticeListBean.DataBeanX> dataBeanList) {
        if (isLoadMore) {
            xrvSystemMsgList.loadMoreComplete();
            if (dataBeanList.size() == 0) {
                xrvSystemMsgList.setNoMore(true);
            } else {
//                noticeList.addAll(dataBeanList);
                systemMsgListAdapter.addData(dataBeanList);
            }
            isLoadMore = false;
        } else {
            if (dataBeanList.size() == 0) {
                flTitleRight.setVisibility(View.GONE);
                xrvSystemMsgList.refreshComplete();

            } else {
                xrvSystemMsgList.refreshComplete();
                if (isRefresh) {
                    noticeList = dataBeanList;
                    xrvSystemMsgList.setNoMore(false);
                    isRefresh = false;
                } else {
                    noticeList = dataBeanList;
                }
            }
            systemMsgListAdapter.setData(dataBeanList);
        }
    }

    public void getNoticeListFail(String error) {

    }

    @Override
    public void onClick(ArrayList<NoticeListBean.DataBeanX> noticeList, int position, boolean isChecked) {
        NoticeListBean.DataBeanX dataBeanX = noticeList.get(position);
        if (isDelete) {
            dataBeanX.setHasSelected(!isChecked);
            this.noticeList.set(position, dataBeanX);
            systemMsgListAdapter.setData(this.noticeList);
        } else {
            dataBeanX.setIs_read("1");
            this.noticeList.set(position, dataBeanX);
            systemMsgListAdapter.setData(this.noticeList);
            getP().noticeRead(dataBeanX.getId());
            int type = InvitationLetterActivity.INVITE;
            Router.newIntent((Activity) App.getInstance()).putString("id", dataBeanX.getExtra().getData().getInvite_id()).putInt("type", type).to(InvitationLetterActivity.class).launch();
        }
    }

    @Override
    public void onLongClick(ArrayList<NoticeListBean.DataBeanX> noticeList, int position, boolean isChecked) {

    }

    public void NoticeDeleteSucceed(String s) {
        T.getInstance().showShort(s);
        for (int index = noticeList.size() - 1; index >= 0; index--) {
            NoticeListBean.DataBeanX dataBeanX = noticeList.get(index);
            if (dataBeanX.isHasSelected()) {
                noticeList.remove(index);
            }
        }
        systemMsgListAdapter.setData(noticeList);
    }

    public void NoticeDeleteFail(String error) {
        T.getInstance().showShort(error);
    }

    public void NoticeReadSucceed(String msg) {

    }

    public void NoticeReadFail(String error) {

    }
}
